/**
 * Created by Lcouncillor on 2020/5/13.
 */
// 购物车水果数量和小计控制

function account_sub(obj){

    var account = $(obj).next().val();
    if (account>1){
        $(obj).next().val(parseInt(account)-1);
    }

    subtotal_prive(obj);
}

function account_add(obj){
    var account = $(obj).prev().val();
    $(obj).prev().val(parseInt(account)+1);

    subtotal_prive(obj);
}

function subtotal_prive(obj){

    var account = parseInt($(obj).parent().children().eq(1).val());
    var unitprice = parseFloat($(obj).parent().prev().children().eq(0).html());

    $(obj).parent().parent().children().eq(5).children().eq(0).html(account*unitprice);
}

// 展开或隐藏购物车水果
$('a.show-hide').click(function(){
    $(this).parent().parent().parent().children().eq(1).toggle()
})


// 结算
function settle_accounts(){
    var fruits = $('input.carter:checked');
    $('span.total-account').html(fruits.length);

    var totalPrice = 0;
    for (var i=0; i<fruits.length; i++){
        totalPrice += parseFloat($(fruits).eq(i).parent().parent().children().eq(5).children().eq(0).html());
    }
    $('span.total-price').html(totalPrice);
}

$('input[type="checkbox"],input.account-sub,input.account-add').click(settle_accounts)

$('ul.fruit-list').click(settle_accounts)

$('ul.fruit-activity-list').click(settle_accounts)

$('input.account').blur(settle_accounts)


//跳转或关闭页面触发购物车数据保存
// window.attachEvent('onunload',function(){window.location.replace('Admin_Quit.asp');});
// function checkLeave(){
//     // event.returnValue="是否保存购物车？";
//
// }


var fruits;
//动态显示购物车数据
$.ajax({
    url:"/shoppingcart/showinfos",
    type:"post",
    dataType:"json",
    success:function (data) {
        fruits = data;
        console.log(data);

        for ( var i=0; i<data.length; i++){
            if ( data[i].activityId == null ){
                var totalPrice = data[i].fruitPrice*data[i].fruitAmount;

                var html = '<li><div class="cart-div1" style="border-bottom: 1px dashed #ddd;">';
                html += '<div class="cart-div2"><input type="checkbox" class="carter" name="chkFruit2" onclick="chkFruit_onclick(this)"/></div>';
                html += '<div class="cart-div3"><img style="height: 100px" src="'+data[i].picUrl+'" alt=""/></div>';
                html += '<div class="cart-div4">';
                html += '<p></p>';
                html += '<p><span>新鲜</span><strong class="fruitname">'+data[i].fruitName+'</strong></p></div>';
                html += '<div class="cart-div5 ">￥<span class="unit-price">'+data[i].fruitPrice+'</span></div>';
                html += '<div class="cart-div6"><input type="button" value="-" class="account-sub" onclick="account_sub(this)" style="width: 25px"/><input type="text" value="'+data[i].fruitAmount+'" class="account" style="width: 55px"/>';
                html += '<input type="button" value="+" class="account-add" onclick="account_add(this)" style="width: 25px"/></div>';
                html += '<div class="cart-div7">￥<span class="subtotal-price">'+totalPrice+'</span></div>';
                html += '<div class="cart-div8"><a href="" data-toggle="modal" data-target="#myModal" onclick="get_deletefruit(this)"><img src="./img/cart/delete.png" alt=""/></a></div></div></li>';

                $('ul.fruit-list').append(html);
            }else{
                var totalPrice = data[i].fruitActivityCost*data[i].fruitAmount;

                var html = '<li><div class="cart-div1" style="border-bottom: 1px dashed #ddd;">';
                html += '<div class="cart-div2"><input type="checkbox" class="carter" name="chkFruit1" onclick="chkFruit_onclick(this)"/></div>';
                html += '<div class="cart-div3"><img style="height: 100px" src="'+data[i].picUrl+'" alt=""/></div>';
                html += '<div class="cart-div4"><p class="activity-style"><strong>'+data[i].activityName+'</strong></p>';

                html += '<p><span>新鲜</span><strong class="fruitname">'+data[i].fruitName+'</strong></p></div>';
                html += '<div class="cart-div5 ">￥<span class="unit-price">'+data[i].fruitActivityCost+'</span><del style="font-size: smaller;padding-left: 10px">￥'+data[i].fruitPrice+'</del></div>';
                html += '<div class="cart-div6"><input type="button" value="-" class="account-sub" onclick="account_sub(this)" style="width: 25px"/><input type="text" value="'+data[i].fruitAmount+'" class="account" style="width: 55px"/>';
                html += '<input type="button" value="+" class="account-add" onclick="account_add(this)" style="width: 25px"/></div>';
                html += '<div class="cart-div7">￥<span class="subtotal-price">'+totalPrice+'</span></div>';
                html += '<div class="cart-div8"><a href="" onclick="get_deletefruit(this)" data-toggle="modal" data-target="#myModal"><img src="./img/cart/delete.png" alt=""/></a></div></div></li>';

                $('ul.fruit-activity-list').append(html)
            }

        }
    }
})

//确定删除对象
var deleteFruit;
function get_deletefruit(obj) {
    var fruitName = $(obj).parent().parent().children().eq(2).children().eq(1).children().eq(1).text();
    for (var j=0; j<fruits.length; j++){
        if(fruitName == fruits[j].fruitName){
            deleteFruit = fruits[j];
            break;
        }
    }
}

//删除购物车
function deletefruit() {
    var cartId = deleteFruit.cartId;
    $('#myModal').modal('hide');
    $.get("/shoppingcart/deletecart?cartId="+cartId,function (data) {
        if (data == "success"){
            alert("删除成功");
            location.reload();
        }
    })
}

//获取选中的水果
function get_payfruits() {
    var payFruits = new Array();
    var fruitNames = $('input.carter:checked');

    for ( var i=0; i<fruitNames.length; i++){
        var fruitName = fruitNames[i].parentElement.parentElement.children[2].children[1].children[1].innerHTML;

        for (var j=0; j<fruits.length; j++){
            if(fruitName == fruits[j].fruitName){
                payFruits.push(fruits[j]);
                break;
            }
        }
    }
    return payFruits;
}

//结算


function fruit_pay() {

    var cartFruits = get_cart();

    $.post({
        url: "/shoppingcart/savecart",
        contentType: "application/json",
        data:JSON.stringify(cartFruits)
    })
    var payFruits = get_payfruits();
    console.log(payFruits);
    // $.post({
    //     url: "/shoppingcart/pay",
    //     contentType: "application/json",
    //     data:JSON.stringify(payFruits),
    //     success:function (data) {
    //         console.log(data)
    //     }
    //
    // })
    // window.location=("./cartpay.html?a="+payFruits);


    //  localStorage 存储
    localStorage.setItem("payfruits", JSON.stringify(payFruits));
    window.location=("./cartpay.html");
}

//获取页面数据
function get_cart() {
    var cartFruits = fruits;
    console.log(fruits);
    for (var i=0; i<fruits.length; i++){
        var fruitNames = $('strong.fruitname').eq(i).html();
        var fruitamount = $('input.account').eq(i).val();
        for (var j=0; j<cartFruits.length; j++){
            if (cartFruits[j].fruitName == fruitNames){
                cartFruits[j].fruitAmount = parseInt(fruitamount);
            }
        }
    }

    return cartFruits;

}


//跳转或关闭页面自动保存购物车数据
window.onbeforeunload=function updatecart(){
    var cartFruits = get_cart();

    $.post({
        url: "/shoppingcart/savecart",
        contentType: "application/json",
        data:JSON.stringify(cartFruits)
    })
}
